package com.project.communitymanagement.common;

import lombok.Data;

/**
 * @ProjectName: hotelNew
 * @Package: com.setup.mysql.common
 * @ClassName: ResponseEntity
 * @Author: "XGP"
 * @Description:
 * @Date: 2021/09/13 11:22
 * @Version: 1.0
 */
@Data
public class ResponseEntity<T> {
    private T data;
    private String status;
    private int code;
    public ResponseEntity(ResponseStatus status,T data){
        this.data=data;
        this.status=status.toString();
        this.code=status.getValue();
    }
    public static ResponseEntity<String> error(){
        return new ResponseEntity(ResponseStatus.ERROR,ErrorCode.ERROR.message());
    }

    public static ResponseEntity<Object> error(Object o){
        return new ResponseEntity(ResponseStatus.ERROR,o);
    }
    public static ResponseEntity<String> success(){
        return success(ResponseStatus.SUCCESS.getMessage());
    }
    public static ResponseEntity error(int code,Object t){
        return new ResponseEntity(code,ResponseStatus.ERROR,t);
    }

    public static ResponseEntity error(ErrorCode t){
        return new ResponseEntity(t.code(),ResponseStatus.ERROR,t.message());
    }
    public static ResponseEntity success(Object t){
        return new ResponseEntity(ResponseStatus.SUCCESS,t);
    }
    public static ResponseEntity success(int code,Object t){
        return new ResponseEntity(code,ResponseStatus.SUCCESS,t);
    }

    /**
     *
     * @param t 数据实体
     * @param dataType 数据类型，若数据是JSON 给空 或者JSON
     * @return
     */
    public static ResponseEntity success(Object t,String dataType){
        if(StringUtil.isNoneEmpty(dataType))
            dataType = DataType.JSON;
        if(!(t instanceof String) && dataType.equals(DataType.JSON))
        {
            return success(StringUtil.Object2Json(t));
        }
        return success(t);
    }
    public ResponseEntity(int code,ResponseStatus status,T data){
        this.data=data;
        this.status=status.toString();
        this.code=code;
    }
    public ResponseEntity(String status,T data){
        this.data=data;
        this.status=status;
    }

    public ResponseEntity(int code,String status,T data){
        this.data=data;
        this.status=status;
        this.code=code;
    }
    public ResponseEntity(){};
    public static class DataType{
        public final static String JSON="JSON";
    }
}
